<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Menu extends AdminController {

	function __construct() { 
		parent::__construct();
		$this->load->model('Menu_model');
		$this->load->library('ProjectService/menu_service');
	}

	public function Index() {	
		$this -> load -> view('admin/menuList');
	}

	function getMenu(){
		$menu=$this->menu_service->getMenuTree(getSess()['park_id']);
		exit(retJson('菜单数据',true,$menu));
	}

	function delete(){
		$sub_menu=$this->Menu_model->get_single(array('pid'=>$_POST['menu_id'],'park_id'=>getSess()['park_id']));
		if($sub_menu){
			exit(retJson('该菜单下还存在未删除的菜单，请先删除子菜单',false));
		}
		$sql=$this->sqlEscape(" delete from db_menu where id=?? and park_id=?? ",array($_POST['menu_id'],getSess()['park_id']));
		$this->Menu_model->query($sql,false);
		exit(retJson('删除成功',true));
	}

	function edit($id,$pid=0){
		$data['id']=$id;
		$data['pid']=$pid;
		$data['detail']=$this->Menu_model->get_single(array('id'=>$id));
		$this->load->view('admin/menuEdit',$data);
	}

	function save(){
		if($_POST['name']==''){
			exit(retJson('请输入菜单名',false));
		}
		$param=$_POST;
		$param['park_id']=getSess()['park_id'];
		if($param['id']==0){
			unset($param['id']);
			$param['created']=time();
			$param['creator']=getSess()['id'];
			$this->Menu_model->add($param);
		}
		else{
			unset($param['pid']);
			$param['updated']=time();
			$param['updator']=getSess()['id'];
			$this->Menu_model->update($param);
			$this->Menu_model->update($param);
		}
		exit(retJson('保存成功',true));
	}






}